<?php
namespace app\tools\logic;
use think\facade\Db;

class ChannelLogic{
	static function getList(){
		$page_size = input('page_size', 10);
		$channel_name = input('channel_name', '');
		$obj = Db::name('channel')
				->alias('c')
				->field('c.*, cc.currency_name,cc.currency_symbol,cc.exchange_rate')
				->join('channel_currency cc','cc.id=c.currency_id','left')
				->where('c.status','<>',127);
		$channel_name && $obj->where('c.channel_name','like','%'.$channel_name.'%');
		$list = $obj->order('c.id desc')
				->paginate($page_size);
		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('channel')
				->alias('c')
				->field('c.*, cc.currency_name,cc.currency_symbol,cc.exchange_rate')
				->join('channel_currency cc','cc.id=c.currency_id','left')
				->where('c.status','<>',127)
				->where('c.id', $id)
				->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['channel_name'] = input('channel_name','');
		$data['base_rate'] = input('base_rate', 1);
		$data['currency_id'] = input('currency_id', 0);
		$data['status'] = input('status', 1);
		$data['remark'] = input('remark', "");
		// $data['create_time'] = input('create_time', 0);



		$rs = Db::name('channel')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			//同意修改通道下的国家和服务的价格基数
			Db::name('channel_country')->where('channel_id', $id)->update(['base_rate'=>$data['base_rate']]);
			Db::name('channel_server')->where('channel_id', $id)->update(['base_rate'=>$data['base_rate']]);
			Db::name('channel_country_server')->where('channel_id', $id)->update(['base_rate'=>$data['base_rate']]);

			
			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['channel_name'] = input('channel_name','');
		$data['currency_id'] = input('currency_id', 0);
		$data['base_rate'] = input('base_rate', 1);
		$data['status'] = input('status', 1);
		$data['remark'] = input('remark', "");
		$data['create_time'] = date('Y-m-d H:i:s');

		$rs = Db::name('channel')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('channel')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}